#coding=utf-8

import json
import commonHelper
import confFileHelper
import tsHelper
import os
import shutil 

jsonFileConf = "conf.json" #配置清单 
jsonData = commonHelper.loadJson(jsonFileConf)
confInputDir = jsonData["inputDir"]  #csv目录
jsOutputPath = jsonData["outputJs"]  #生成的路径
genTsDir = jsonData["outputTsDir"]  #ts文件输出目录

#清除掉ts目录
commonHelper.clearDir(genTsDir) 
# print(jsOutputPath)
#初始化目录 
commonHelper.initFileDir(jsOutputPath)
commonHelper.initFileDir(genTsDir)

allConfData = {} 

for (i,fileConf) in enumerate(jsonData["files"]):
    confName = fileConf["name"].replace(".csv", "").replace("t_base_", "")
    confName = commonHelper.parseProperites(confName) #这是配置文件名，转成驼峰命名



    fileReader = open(confInputDir + fileConf["name"], "r+", encoding="utf-8")
    print(fileReader.name)
    # 第一行到第三行的配置
    line = commonHelper.fileReadLine(fileReader)  # 标题中文名
    cnTitles = line.split(",")
    titleTypes = commonHelper.splitConfLine(commonHelper.fileReadLine(fileReader))  # 字段类型
    line = commonHelper.fileReadLine(fileReader)  # 字段名
    titleNames = confFileHelper.converTitle(commonHelper.splitConfLine(line))
    keysList = confFileHelper.converKeys(fileConf["keys"])
    keys2ArrayList = confFileHelper.converKeys(fileConf["keys2Array"])
    colNameDic = confFileHelper.getColNameDic(line) #列名索引
    confData = confFileHelper.dealOneFile(fileReader, keysList,keys2ArrayList, titleTypes, titleNames, colNameDic) # 处理除表头文件外的其他数据
    fileReader.close()
    allConfData[confName] = confData

    # 生成ts文件
    tsHelper.genTsFile(genTsDir, confName , keysList,keys2ArrayList, titleTypes ,colNameDic, cnTitles, fileConf["desc"])

outFile = open(jsOutputPath, 'w+', encoding='utf-8')
outFile.write("GameConfData = " + json.dumps(allConfData))
outFile.close()

#  压缩配置文件
# zlibHelper.compress(jsOutputPath,jsCompressedOutput)

